#include "Os_HwInit.h"

#include "app_common.h"
#include "app_hw_i2c.h"
#include "app_uart.h"
#include "app_rtc.h"
#include "app_adc.h"


/* Debug print enabled */
#define INIT_OS_DEBUG

#ifdef INIT_OS_DEBUG
#include <stdio.h>
#endif

/* Os_HwInit entry function */
/* pvParameters contains TaskHandle_t */
void Os_HwInit_entry(void * pvParameters)
{
	FSP_PARAMETER_NOT_USED(pvParameters);

	/* TODO: add your own code here */
	EventBits_t event_group_bits;
	EventBits_t setting_event_group;
	
	/* Hardware initialization */
	R_BSP_PinAccessEnable();;
	Uart_Init();
	Rtc_Init();
	Adc_Init();
	I2c_Init();
	
	Eeprom_I2c_Init();
	// Read saved data in EEPROM
	Eeprom_ReadKeyData();
	
#ifdef INIT_OS_DEBUG
	/* printf for avoid garbled code */
	printf("\r\n"); 
	printf("=== Demo Test, Compiler@[%s, %s] ===\r\n", __DATE__, __TIME__);
#endif
	
	/* used for flag the initialize is finished */
	// Set event group 0 bit0 to halt other tasks
	setting_event_group = EVENT_GROUP_SET_BIT(EVENT_GROUP_0_INIT);
	event_group_bits = xEventGroupSetBits(event_group_0, setting_event_group);
	if ((event_group_bits & setting_event_group) == 0)
	{
		printf ("event group 0 setting error - init\n");
	}
	
	// delete the initialize task
	extern TaskHandle_t Os_HwInit;
	vTaskDelete(Os_HwInit);
	
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(1000));
	}
}
